package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IAppMarkService;
import com.younion.vo.AppMarkVO;

/**
 * @author wells
*/
public class AppMarkAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(AppMarkAction.class);

	private IAppMarkService appMarkService = null;

	private AppMarkVO appMarkVO = new AppMarkVO();

	private List<AppMarkVO> appMarkList; 
	/**
	 *  列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listAppMark() {
		try {
			Map paramMap = null;
			if (appMarkVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(appMarkVO));
			}
			// 获取总记录数 
			int total = appMarkService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象 
			Paginator paginator = this.getPaginator(total);
			appMarkList = appMarkService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 *  新增
	 */
	public String addAppMark() {
		return SUCCESS;
	}

	/**
	 *  修改保存，调度Jsp
	 */
	public String editAppMark() {
		try {
			appMarkVO = appMarkService.getById(appMarkVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 * @return
	 */
	public String saveAppMark() {
		try {
			int id = appMarkService.saveReturnKey(appMarkVO);
			ImageFileUtil.processImageFiles(id, "1", request);
			String log_id = appMarkVO.getId() == null ? "" : appMarkVO.getId().toString();
			String log = "".equals(log_id) ? "添加应用推荐" : "更新应用推荐";
			int type = "".equals(log_id) ? 3:4;
			this.addLog(type, log+log_id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteAppMark() {
		try {
			String pkId = request.getParameter("key");
			appMarkService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除应用推荐";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewAppMark() {
		try {
			String pkId = request.getParameter("key"); 
			appMarkVO = appMarkService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public AppMarkVO getAppMarkVO() {
		return appMarkVO;
	}

	public void setAppMarkVO(AppMarkVO appMarkVO) {
		this.appMarkVO = appMarkVO;
	}

	public void setAppMarkService(IAppMarkService appMarkService) {
		this.appMarkService = appMarkService;
	}

	public List<AppMarkVO> getAppMarkList() {
		return appMarkList;
	}

	public void setAppMarkList(List<AppMarkVO> appMarkList) {
		this.appMarkList = appMarkList;
	}

}